package com.example.guli.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.guli.common.base.result.R;
import com.example.guli.service.edu.entity.vo.TeacherQueryVo;
import com.example.guli.service.edu.entity.Teacher;
import com.example.guli.service.edu.feign.OssFileService;
import com.example.guli.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author shunxuan
 * @since 2020-07-16
 */
@Api(description = "01讲师管理")
@CrossOrigin//允许跨域
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public  class TeacherController {
    @Autowired//依赖注入TeacherService
    private TeacherService teacherService;
    @Autowired//依赖注入oss服务调用的OssFileService接口
    private OssFileService ossFileService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    //获取所有的讲师列表
//    public List<Teacher> listAll(){
//        List<Teacher> list = teacherService.list();
//        return list;
//    }
    public R listAll(){
        List<Teacher> list=teacherService.list();
        return R.ok().data("data",list).message("获取讲师列表成功");
    }

    @ApiOperation("讲师分页列表")
    @PostMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码",required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数",required = true) @PathVariable Long limit,
                      @ApiParam(value = "查询对象") TeacherQueryVo teacherQueryVo
                      ){
        Page<Teacher> pageParam = new Page<>(page, limit);
//        IPage<Teacher> pageModel=teacherService.page(pageParam);
        //自定义分页方法selectPage,先进入teacherService的接口，然后再进入其实现类书写逻辑
        IPage<Teacher> pageModel=teacherService.selectPage(pageParam,teacherQueryVo);
        List<Teacher> records = pageModel.getRecords();
        long total=pageModel.getTotal();
        long current=pageModel.getCurrent();
        long pageSize=pageModel.getSize();
        long pages=pageModel.getPages();
        return R.ok().data("total",total).data("current",current).data("pageSize",pageSize).data("pages",pages).data("data",records);
    }


    @ApiOperation(value="根据id删除讲师",notes = "根据id删除讲师，逻辑删除")
    @DeleteMapping("remove/{id}")
//    public boolean removeById(@ApiParam("讲师id") @PathVariable String id){
//        return teacherService.removeById(id);
//    }
//    public R removeById(@ApiParam(value = "讲师id",required = true) @PathVariable String id){
//        boolean b = teacherService.removeById(id);
//        return b?R.ok().message("删除成功"):R.error().message("数据不存在");
//    }
    public R removeById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){
        //删除图片，需要调用oss微服务
        teacherService.removeAvatarById(id);
        //删除讲师
        boolean result=teacherService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }



    //时间自动填充：com.example.guli.service.base.handler
    //方法一：将具有唯一约束的列，取消唯一性约束；
    //方法二：保证插入的数据，与表中已有的数据不重复！
    @ApiOperation(value = "新增讲师",notes = "使用时间的自动填充插件，所以不需要时间字段")
    @PostMapping("save")
    public R save(@ApiParam("讲师对象") @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if(result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }

    }

    @ApiOperation(value="更新讲师",notes = "使用时间的自动填充插件，所以不需要时间字段")
    @PutMapping("update")
    public R updateById(@ApiParam("讲师对象") @RequestBody Teacher  teacher){
        boolean result = teacherService.updateById(teacher);
        if(result){
            return R.ok().message("更新成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id获取讲师信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师id",required = true) @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if(teacher!=null){
            return R.ok().data("data",teacher);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value="根据id列表批量删除讲师",notes = "根据id列表删除讲师，逻辑删除：[1311110735591825409,1311110735591825409" +
            "]")
    @DeleteMapping("batch-remove")
    public R removeRows(@ApiParam(value = "讲师id列表",required = true)
                      @RequestBody   List<String> idList){
        boolean b = teacherService.removeByIds(idList);
        return b?R.ok().message("删除成功"):R.error().message("数据不存在");
    }

    @ApiOperation("根据关键字查询讲师名列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(@ApiParam(value = "查询关键字",required = true) @PathVariable String key){
        //查询指定的字段，过滤为null的值，必须使用Map集合才能实现
        List<Map<String,Object>> nameList=teacherService.selectNameList(key);
        return R.ok().data("data",nameList);
    }


    @ApiOperation("测试oss服务调用-服务的消费者")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        log.info("调用oss");
        return R.ok();
    }

    @ApiOperation("测试并发")
    @GetMapping("test_concurrent")
    public R testConcurrent(){
        log.info("调用oss，测试并发");
        return R.ok();
    }

    //Sentinel服务容错测试
    @GetMapping("/message1")
    public String message1() {
        return "message1";
    }
    @GetMapping("/message2")
    public String message2() {
        return "message2";
    }


}

